package com.qtummatrix.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.opensymphony.xwork2.ActionSupport;
import com.qtummatrix.entity.ModelEntity;
import com.qtummatrix.entity.MoudleEntity;
import com.qtummatrix.entity.QuestionsEntity;
import com.qtummatrix.service.QuestionsMoudleService;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("questionsMoudleAction")
@Scope("prototype")
public class QuestionsMoudleController extends ActionSupport {
    Map<String,Object> info=new HashMap<>();
    private MoudleEntity moudleEntity;

    public MoudleEntity getMoudleEntity() {
        return moudleEntity;
    }

    public void setMoudleEntity(MoudleEntity moudleEntity) {
        this.moudleEntity = moudleEntity;
    }

    public QuestionsMoudleService getQuestionsMoudleService() {
        return questionsMoudleService;
    }

    public void setQuestionsMoudleService(QuestionsMoudleService questionsMoudleService) {
        this.questionsMoudleService = questionsMoudleService;
    }

    public Map<String, Object> getInfo() {
        return info;
    }

    public void setInfo(Map<String, Object> info) {
        this.info = info;
    }

    @Autowired
    @Qualifier("questionsMoudleService")
    private QuestionsMoudleService questionsMoudleService;

    public String get_QuestionsByScope(){
        HttpServletRequest request= ServletActionContext.getRequest();
        int scopeid=Integer.parseInt(request.getParameter("scopeid"));
        List<QuestionsEntity>list = questionsMoudleService.get_QuestionsByScope(scopeid);
        info.put("questionsByScopeList",list);
        return SUCCESS;
    }
    public String addQuestionMoudle(){
        HttpServletRequest request=ServletActionContext.getRequest();
        String questionsid=request.getParameter("questionsid");
        JSONArray obj= JSON.parseArray(questionsid);
        int []arr=new int[10];
        for (int i=0;i<obj.size();i++){
            arr[i]=Integer.parseInt((String)obj.get(i));
        }
        String str=questionsMoudleService.add_QuestionsMoudle(moudleEntity,arr);
        info.put("msg",str);
        return SUCCESS;
    }

    public String getMoudles(){
        HttpServletRequest request=ServletActionContext.getRequest();
        String name=request.getParameter("name");
        int scopeid=Integer.parseInt(request.getParameter("scopeid"));
        int n=Integer.parseInt(request.getParameter("n"));
        List<MoudleEntity> list=questionsMoudleService.get_Moudles(scopeid,name,n);
        info.put("moudlelist",list);
        return SUCCESS;
    }

    public String getSumPage(){
        HttpServletRequest request=ServletActionContext.getRequest();
        String name=request.getParameter("name");
        int scopeid=Integer.parseInt(request.getParameter("scopeid"));
        info.put("moudlesumpage",questionsMoudleService.get_MoudleSumpage(scopeid,name));

        return SUCCESS;
    }

    public String getQuestionsInfoByMoudleID(){
        HttpServletRequest request=ServletActionContext.getRequest();

        int moudleid=Integer.parseInt(request.getParameter("id"));
        List<QuestionsEntity> list=questionsMoudleService.get_QuestionsInfoByMoudleID(moudleid);

        info.put("infolist",list);

        return SUCCESS;

    }

    public String delMoudle(){
        HttpServletRequest request=ServletActionContext.getRequest();
        int moudleid=Integer.parseInt(request.getParameter("moudleid"));
        questionsMoudleService.delmoudle(moudleid);
        info.put("msg","删除成功！");
        return SUCCESS;
    }

    public String getGoodsInfo(){
        Map<Integer,Object>map= questionsMoudleService.getGoodsMoudleInfo();
        List<ModelEntity> modelentities=(List<ModelEntity>)map.get(0);
        List<String> stringList= (List<String>) map.get(1);
        info.put("modellist",modelentities);
        info.put("stringlist",stringList);
        return SUCCESS;
    }

    public String getAllMoudle(){

        List<MoudleEntity> list=questionsMoudleService.get_AllMoudle();
        info.put("allmoudle",list);
        return SUCCESS;
    }

    public String updateGoodsMoudle(){
        HttpServletRequest request=ServletActionContext.getRequest();
        int id=Integer.parseInt(request.getParameter("id"));
        int moudleid=Integer.parseInt(request.getParameter("moudleid"));
        questionsMoudleService.updateGoodsMoudle(id,moudleid);
        info.put("msg","保存成功");
        return SUCCESS;
    }
}
